/**
 * 
 */
package com.jm.web.builder.table;

import java.io.IOException;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;

import com.jm.utils.StringBuilderEx;
import com.jm.utils.StringHelper;
import com.jm.utils.Utils;
import com.jm.web.builder.BaseBuilder;
import com.jm.web.data.BaseDataEntity;
import com.jm.web.data.ReturnData;
import com.jm.web.form.table.Td;
import com.jm.web.tags.support.BaseTag;
import com.jm.web.tags.table.TbodyTag;
import com.jm.web.tags.table.TdTag;

/**
 * @author yanjie
 * Jan 30, 2012 2:35:18 PM
 */
public class TdBuilder extends BaseBuilder{
	TdTag tdTag = null;
	TbodyTag tbodydTag = null;
	Utils utils = null;
	ReturnData ret = null;
	Object row = null;
	String cid = "";
	String tabid = "";
	BaseDataEntity config = null;
	String value = "";
	String text = "";
	boolean buserLastTd = false;
	
	@Override
	public void init(BaseTag tag) {
		super.init(tag);
		
		utils = this.getBaseTag().getPage().getUtils();
		ret = utils.getReturnData();
		if(ret != null)
			row = ret.getTableRow();
		cid = this.getBaseTag().GetExtValue("cid","");
		
		if(this.getBaseTag() instanceof TdTag){
			tdTag = (TdTag)this.getBaseTag();
			tbodydTag = tdTag.getTrTag().getTbodyTag();
		}
		
		if(tdTag.getTrTag().getTbodyTag() != null ){
			config = tdTag.getTrTag().getTbodyTag().getTableTag().getTable().getColConfig(cid);
			tabid = tdTag.getTrTag().getTbodyTag().getTableTag().getTable().getId();
		}
		
	}
	
	@Override
	public void RenderStart() {
		try {
			if(!StringHelper.IsNullOrEmpty(cid)  && row != null ){
				if("rownumber".equals(cid)){
					value = ""+(ret.getTableRowIndex()+1);
				}else if(row instanceof ResultSet){
					try{
						value = utils.getRsString(((ResultSet)row),cid);
					}catch(Exception e){}
				}else if(row instanceof Map){
					value = (String)((Map)row).get(cid);
				}else if(row instanceof BaseDataEntity){
					value = ((BaseDataEntity)row).gpsv(cid);
				}
				text = value;
			}else{
				if("rownumber".equals(cid)){
					value = ""+(ret.getTableRowIndex()+1);
					text = value;
				}
			}
			
			StringBuilderEx sb = new StringBuilderEx();
			sb.Append("<td ");
			if(!StringHelper.IsNullOrEmpty(cid)){
				sb.Append(" cid = \"%1$s\" ",cid);
			}
			
			if(!StringHelper.IsNullOrEmpty(this.getBaseTag().GetExtValue("align",""))){
				sb.Append(" align = \"%1$s\" ",this.getBaseTag().GetExtValue("align",""));
			}
			

			if(!StringHelper.IsNullOrEmpty(this.getBaseTag().GetExtValue("title",""))){
				sb.Append(" title = \"%1$s\" ",this.getBaseTag().GetExtValue("title",""));
			}
			
			if (!StringHelper.IsNullOrEmpty(this.getBaseTag().GetExtValue("ondblclick",""))){
				sb.Append(" ondblclick = \"%1$s\"",
						this.getBaseTag().GetExtValue("ondblclick",""));
			}
			
			if(!StringHelper.IsNullOrEmpty(this.getBaseTag().GetExtValue("colspan",""))){
				sb.Append(" colspan = \"%1$s\" ",this.getBaseTag().GetExtValue("colspan",""));
			}
			
			if(!StringHelper.IsNullOrEmpty(this.getBaseTag().GetExtValue("rowspan",""))){
				sb.Append(" rowspan = \"%1$s\" ",this.getBaseTag().GetExtValue("rowspan",""));
			}
			
			if(!StringHelper.IsNullOrEmpty(this.getBaseTag().GetExtValue("style",""))){
				sb.Append(" style = \"%1$s\" ",this.getBaseTag().GetExtValue("style",""));
			}
			
			if(!StringHelper.IsNullOrEmpty(this.getBaseTag().GetExtValue("class",""))){
				sb.Append(" class = \"%1$s\" ",this.getBaseTag().GetExtValue("class",""));
			}
			
			if(this.getBaseTag().getPage().isExport() && !StringHelper.IsNullOrEmpty(this.getBaseTag().GetExtValue("exportconfig",""))){
				sb.Append(" exportconfig = \"%1$s\" ",this.getBaseTag().GetExtValue("exportconfig",""));
			}
			
			if (!StringHelper.IsNullOrEmpty(this.getBaseTag().GetExtValue("extAtr",""))){
				sb.Append(" %1$s",
						this.getBaseTag().GetExtValue("extAtr",""));
			}
			
			if(ret != null && ret.getTableRow() != null){
				sb.Append(" index = \"%1$s\"",ret.getTableRowIndex()+1);
				sb.Append(" rowindex = \"%1$s\"",ret.getTableRowIndex());
			}
			
			if(this.tbodydTag.getTableTag().GetExtValue("edit", true) && config != null){
				if(config.ContainesParam("editor") && !config.isNullOrEmpty("editor")){
					//开启编辑模式
					sb.Append(" edit = \"true\"");
					sb.Append(" editor = \"%1$s\"",config.gpsv("editor"));
					sb.Append(" colindex = \"%1$s\"",config.gpsv("colIndex"));
					
					
					if(config.gpsv("editor").equals("select")){
						sb.Append(" value = \"%1$s\"",value);
						Map map = (HashMap)this.getBaseTag().getPage().getListItemData(config.gpsv("editorid"));
						if(map != null){
							text = (String)map.get(value);
						}
					}else if(config.gpsv("editor").equals("org")){
						sb.Append(" value = \"%1$s\"",value);
						if(!StringHelper.IsNullOrEmpty(cid)){
							text = ret.grsv(cid+"_text");
						}
					}else if(config.gpsv("editor").equals("combobox")){
						sb.Append(" value = \"%1$s\"",value);
//						if(!StringHelper.IsNullOrEmpty(cid) && ret.grsv(cid+"_text").length()>0){
//							text = ret.grsv(cid+"_text");
//						}
					}
					
					sb.Append(" onclick = \"$P.editorControl({me:this,name:'e_%1$s',index:'%2$s',tabid:'%3$s',colconfig:%4$s})\" ",
							cid,
							config.gpsv("colIndex"),
							tabid,
							config.ToJSONString().replaceAll("\"","'"));
					
				}
			}
			
			if(this.tbodydTag.isAutoMerge()){
				if(this.tdTag.isAutoMerge1()){
					String groupValue =  this.tbodydTag.getGroupValue();
					String lastGroupValue = this.tbodydTag.getLastGroupValue();
					if(!StringHelper.IsNullOrEmpty(lastGroupValue) && lastGroupValue.equalsIgnoreCase(groupValue)){
						buserLastTd = true;
						Td td = this.tbodydTag.getLastTd(cid);
						td.setRowspan(td.getRowspan()+1);
					}
				}else if(this.tdTag.isAutoMerge2()){
					String subGroupValue =  this.tbodydTag.getGroupValue(this.tdTag.getSubGroup());
					String lastSubGroupValue = this.tbodydTag.getLastGroupValue(this.tdTag.getSubGroup());
					if(!StringHelper.IsNullOrEmpty(lastSubGroupValue) && lastSubGroupValue.equalsIgnoreCase(subGroupValue)){
						buserLastTd = true;
						Td td = this.tbodydTag.getLastTd(cid);
						td.setRowspan(td.getRowspan()+1);
					}
				}
				if(!buserLastTd){
					Td td = new Td();
					String group = "emptygroup";
					if(this.tdTag.isAutoMerge1())
						group = "basegroup";
					else if(this.tdTag.isAutoMerge2())
						group = this.tdTag.getSubGroup();
					
					//處理小計,合計
					if(this.tbodydTag.isSubtotal()){
//						if("basegroup".equals(group)){
//							td.setGroupLev(0);
//						}else if(group.startsWith("group")){
//							td.setGroupLev(Integer.parseInt(group.replace("group","")));
//						}
					}
					
					td.SetExtValue("mergeindex",String.valueOf(this.tbodydTag.getTdIndex().gpiv(group)));
					
					td.setTdBeginHtml(sb.toString());
					this.tbodydTag.getLastTr().addTd(td);
					this.tbodydTag.getLastTdMap().put(cid,td);
					this.tbodydTag.getTdIndex().SetParamValue(
							StringHelper.getValue(cid,"temp_id"),
							this.tbodydTag.getTdIndex().gpiv(StringHelper.getValue(cid,"temp_id"))+1);
				}
			}else{
				sb.Append(">");
				this.getOut().write(sb.toString());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void RenderBody() {
		try {
			if( this.getBaseTag().getBodyContent() != null ){
				//合并單元格
				if(this.tbodydTag.isAutoMerge()){
					if(!buserLastTd  || !this.tdTag.isAutoMerge())
						this.tbodydTag.getLastTr().getLastTd().setTdBodyHtml(this.getBaseTag().getBodyContent().getString());
				}else{
					this.getOut().write(this.getBaseTag().getBodyContent().getString());
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void RenderEnd() {
		try {
			if(this.tbodydTag.isAutoMerge()){
				if(!buserLastTd || !this.tdTag.isAutoMerge()){
					//合并單元格
					if(!StringHelper.IsNullOrEmpty(cid) && this.getBaseTag().getBodyContent() == null && (row != null || "rownumber".equals(cid))){
						this.tbodydTag.getLastTr().getLastTd().setTdEndHtml(text+"</td>");
					}else{
						this.tbodydTag.getLastTr().getLastTd().setTdEndHtml("</td>");
					}
				}
			}else{
				if(!StringHelper.IsNullOrEmpty(cid) 
							&& this.getBaseTag().getBodyContent() == null 
							&& (row != null || "rownumber".equals(cid))
							&& text != null
				){
					this.getOut().write(text);
				}
				this.getOut().write("</td>");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
